package configurator

import (
	"iris-starter/bootstrap"
	"iris-starter/config"
	"os"
	"path/filepath"
	"time"
)

func ConfigureLogger(b *bootstrap.Bootstrapper) {
	logConf := config.Conf.Log
	b.Logger().SetLevel(logConf.LoggerLevel)
	b.Logger().SetOutput(newLogFile(logConf))
}

func newLogFile(c config.LogConfig) *os.File {
	filename := filepath.Join(c.LoggerFile, time.Now().Format("20060102")+".log")
	// Open the file, this will append to the today's file if server restarted.
	f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		panic(err)
	}

	return f
}
